<%@page import="multiface.web.util.Util"%>
<%@page import="multiface.awwadco.jpa.Utente"%>
<%
    Utente ut = (Utente) session.getAttribute("admin");
    if (ut == null) {
        response.sendRedirect(multiface.web.util.ServletUtils.getBaseUrl(request) + "/admin/index.jsp?err=y");
    }
%>

<%@page import="java.util.Date"%>
<%@page import="multiface.awwadco.jpa.*"%>
<%@page import="javax.persistence.Persistence"%>
<%@page import="javax.persistence.EntityManager"%>
<%@page import="javax.persistence.EntityManagerFactory"%>
<%@page import="multiface.awwadco.jpa.PersistenceManager"%>
<%
    //if (session.getAttribute("admin") != null) {
    EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
    EntityManager em = emf.createEntityManager();

    em.getTransaction().begin();

    Prodotto p = em.createNamedQuery("Prodotto.findByIdprodotto", Prodotto.class).setParameter("idprodotto", Integer.parseInt(request.getParameter("pid"))).getSingleResult();

    p.setOfferta(!p.getOfferta());




    boolean addNews = false;
    String add_news_p = request.getParameter("addnews");

    if (add_news_p != null) {
        addNews = Boolean.parseBoolean(add_news_p);
    }


    if (p.getOfferta()) {
        out.print("promo.png");
        if (addNews) {
            Util.addNewsPromo(ut, p);
            
        }
    } else {
        out.print("notpromo.png");
    }


    em.persist(p);
    em.getTransaction().commit();

    em.close();
    //emf.close();
    //}
%>
